package com.jinzhi.jzweb.controller.api;

/**
 * @author mengzhichao
 * @create 2023-05-06-9:37
 */

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzapi.utils.JWTUtil;
import com.jinzhi.jzapi.utils.RedisUtil;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.domain.vo.AuntUserVO;
import com.jinzhi.jzweb.service.JazSysLabelService;
import com.jinzhi.jzweb.service.JazSysProjectService;
import com.jinzhi.jzweb.service.JazUserCollectService;
import com.jinzhi.jzweb.service.JazUserOrderService;
import com.jinzhi.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 收藏相关接口
 * @author mengzhichao
 * @create 2023-04-27-13:54
 */
@RestController
@RequestMapping("/jzApi/JazUserCollect")
public class ApiJazUserCollectController {


    @Autowired
    private JazUserCollectService jazUserCollectService; //收藏表

    @Autowired
    private UserService userService; //用户表


    @Autowired
    private JazUserOrderService jazUserOrderService; //签约订单表


    @Autowired
    private JazSysLabelService jazSysLabelService; //标签表

    @Autowired
    private JazSysProjectService jazSysProjectService; //服务表


    /**
     * 收藏
     * @return
     */
    @PostMapping("/collect")
    public Result<?> collect(String auntAndDemandId,Integer type,String projectId){

        UserDO getUser = JWTUtil.getUser();
		UserDO userDO = userService.selectById(getUser.getId());
//        UserDO userDO = userService.selectById(1328881670693347330L); //需求发布人

        JazUserCollectDO jazUserCollectDO =new JazUserCollectDO();
        jazUserCollectDO.setSysUserid(userDO.getId());
        jazUserCollectDO.setAuntAndDemandId(Long.valueOf(auntAndDemandId));
        jazUserCollectDO.setType(type);
        jazUserCollectDO.setCollectProjectId(Long.valueOf(projectId));
        jazUserCollectDO.setCollectionTime(new Date());
        jazUserCollectService.insert(jazUserCollectDO);
        return Result.ok(jazUserCollectDO);
    }




    /**
     * 取消收藏
     * @return
     */
    @PostMapping("/cancelCollect")
    public Result<?> cancelCollect(String id){
        jazUserCollectService.deleteById(Long.valueOf(id));
        return Result.ok();
    }







    /**
     * 查询该用户是否收藏
     * auntId：月嫂id
     * @return
     */
    @GetMapping("/isCollect")
    public Result<?> isCollect(String auntId){
        UserDO getUser = JWTUtil.getUser();
        UserDO userDO = userService.selectById(getUser.getId());
        EntityWrapper<JazUserCollectDO> wrapper = new EntityWrapper<JazUserCollectDO>();
        wrapper.eq("sys_userid",userDO.getId()); //用户id
        wrapper.eq("aunt_and_demand_id",auntId); //月嫂id
        JazUserCollectDO jazUserCollectDO = jazUserCollectService.selectOne(wrapper);
        if (jazUserCollectDO != null){
            jazUserCollectDO.setIs(1); //已收藏
            return Result.ok(jazUserCollectDO);
        }else {
            jazUserCollectDO =new JazUserCollectDO();
            jazUserCollectDO.setIs(0); //未收藏
            return Result.ok(jazUserCollectDO);
        }
    }






    /**
     * 查看月嫂收藏列表
     * @return
     */
    @GetMapping("/selCollectAunt")
    public Result<?> selCollectAunt() throws Exception{

        UserDO getUser = JWTUtil.getUser();
		UserDO userDO = userService.selectById(getUser.getId());

        List<AuntUserVO> rstAuntUserVOs = jazUserCollectService.selCollectAunt(String.valueOf(userDO.getId()));

        for (int j=0;j<rstAuntUserVOs.size();j++){
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

            if (rstAuntUserVOs.get(j).getSigningStatus() == 1){ //已经签约的用户 查询剩余签约天数
                EntityWrapper<JazUserOrderDO> wrapper = new EntityWrapper<JazUserOrderDO>();
                wrapper.eq("initiate_id",rstAuntUserVOs.get(j).getSysUserid()).or().eq("signing_id",rstAuntUserVOs.get(j).getSysUserid());
                wrapper.andNew().eq("conclude_status",2); //已经签署
                wrapper.andNew().ne("status",3);//不等于已取消
                wrapper.andNew().ne("status",2);//不等于已取消
                Date endTime = jazUserOrderService.selectOne(wrapper).getEndTime();
                int dayBetweenTwoDate = daysBetween(format.format(new Date()),format.format(endTime));
                rstAuntUserVOs.get(j).setSigningDate(dayBetweenTwoDate);
            }


            if (StringUtil.notEmpty(rstAuntUserVOs.get(j).getLabelId())) {
                //多个标签查询
                List<String> labelList = splitStrArrayList(rstAuntUserVOs.get(j).getLabelId());
                rstAuntUserVOs.get(j).setLabelId(""); //清空标签id值
                for (int i = 0; i < labelList.size(); i++) {
                    JazSysLabelDO jazSysLabelDO = jazSysLabelService.selectById(labelList.get(i)); //查询标签表
                    if ("".equals(rstAuntUserVOs.get(j).getLabelId())) { //如果labelid是空的
                        rstAuntUserVOs.get(j).setLabelId(jazSysLabelDO.getDescribe());
                    } else {
                        String le = rstAuntUserVOs.get(j).getLabelId() + "," + jazSysLabelDO.getDescribe();
                        rstAuntUserVOs.get(j).setLabelId(le);
                    }
                }
            }

            if (StringUtil.notEmpty(rstAuntUserVOs.get(j).getProjectId())) {
                //多个服务查询
                List<String> projectList = splitStrArrayList(rstAuntUserVOs.get(j).getProjectId());
                rstAuntUserVOs.get(j).setProjectId(""); //清空服务id值
                for (int i = 0; i < projectList.size(); i++) {
                    JazSysProjectDO jazSysProjectDO = jazSysProjectService.selectById(projectList.get(i));//查询服务表
                    if ("".equals(rstAuntUserVOs.get(j).getProjectId())) { //如果ProjectId是空的
                        rstAuntUserVOs.get(j).setProjectId(jazSysProjectDO.getName());
                    } else {
                        String pr = rstAuntUserVOs.get(j).getProjectId() + "," + jazSysProjectDO.getName();
                        rstAuntUserVOs.get(j).setProjectId(pr);
                    }
                }
            }

            rstAuntUserVOs.get(j).setProjectName(jazSysProjectService.selectById(rstAuntUserVOs.get(j).getCollectProjectId()).getName());

//            rstAuntUserVOs.get(j).setName(rstAuntUserVOs.get(j).getName().substring(0,1) + "阿姨");

            rstAuntUserVOs.get(j).setLabelIds(splitStrArrayList(rstAuntUserVOs.get(j).getLabelId()));
        }
        return Result.ok(rstAuntUserVOs);
    }


    /**
     * JAVA计算两个日期相差多少天
     * @param date1str
     * @param date2str
     * @return
     */
    public static int daysBetween(String date1str,String date2str) throws ParseException {
        int count = 0;
        if("".equals(date1str) || date1str == null || "".equals(date2str) || date2str == null) {
            return count;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        Date date1 = format.parse(date1str);
        Date date2 = format.parse(date2str);
        count = Math.abs((int) ((date1.getTime() - date2.getTime()) / (1000*3600*24)));
        return count;
    }



    private static String bs = ",";


    private static List<String> splitStrArrayList(String str) {
        List<String> stringList = new ArrayList<>();
        if (str != null) {
            String[] strs = str.split(bs);
            stringList.addAll(Arrays.asList(strs));
            return stringList;
        }
        return null;
    }



    /**
     * 查看需求收藏列表
     * @return
     */
    @GetMapping("/selCollectDemand")
    public Result<?> selCollectDemand(){

        UserDO getUser = JWTUtil.getUser();
		UserDO userDO = userService.selectById(getUser.getId());
        List<JazUserDemandDO> jazUserDemandDOS = jazUserCollectService.selCollectDemand(String.valueOf(userDO.getId()));
        for (int i=0;i<jazUserDemandDOS.size();i++){
            JazSysProjectDO jazSysProjectDO = jazSysProjectService.selectById(jazUserDemandDOS.get(i).getProjectId());//查询服务表
            jazUserDemandDOS.get(i).setProjectName(jazSysProjectDO.getName());
        }
        return Result.ok(jazUserDemandDOS);
    }

}
